import QtQuick 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"


//hd ready : 1280×720
Item {
     width: 1280
     height: 720
     id:mainItem
    
    function hideHome(){
	console.log('hidding home');
	appControl.hide();
    }
    
    /*MouseArea {
	  anchors.fill: parent
	  onClicked: { 
	    console.log('tube launcher: ' + launchers.getLauncher('youtube'));
	  }
      }*/

    Component.onCompleted: {
      launchers.createLaunchers();
      widgetGridV.createWidgets();
    }
    
    Image {
	source: "images/Abstract_Twitter_Background.jpg"
	fillMode: Image.PreserveAspectFit
	anchors.centerIn: parent//widgetGridV.contentItem
    }
    
    
    NavigationView {
      id:widgetGridV
      anchors.fill: parent
      //cellWidth: 426; cellHeight: 360 //ici on regle l apparence/place grid en fonction de son contenu
      //anchors.centerIn:parent
      focus:true
      /*model : VisualItemModel {
		    id: widgetModel
	      }*/
      
      QmlSetting{
	id : vModel
	appName:'homeDev'
      }
      function addWidget(filePath,name){
	  console.log('add widget:' + name + ' file: ' + filePath);
	  var compt = Qt.createComponent(filePath);
	  var w = compt.createObject(contentItem);
	  //console.log('lLaunchers:'+launchers);
	  w.init(vModel,launchers)
	  w.focus=true;
	  w.hideHome.connect(mainItem.hideHome);
      }
      
      function createWidgets(){
	  console.log('=================>DEBUT creation widgets');
	  var l = vModel.getListKeys('widgets');
	  vModel.beginGroup('widgets');
	  for(var i = 0;i<l.length;i++){
	    var filePath = vModel.value(l[i]+'/widgetUrl');
	    vModel.beginGroup(l[i]);
	    addWidget(filePath,l[i]);
	    vModel.endGroup();
	  }
	  vModel.endGroup();
	  console.log('=================>FIN creation widgets');
      }
      
      
      
    }
    
    //liste des launchers
    Item {
	//properties
	id: launchers
	property variant compt : Qt.createComponent('LauncherApp.qml');
	
	//composants
	QmlSetting {
	    id:v
	    appName:'homeDev'
	}
        
	
	//functions
	function createLaunchers(){
	    console.log('=================>DEBUT creation launchers');
	    var l = v.getListKeys('launchers');
	    v.beginGroup('launchers');
	      for(var i = 0;i<l.length;i++){
		console.log('apps:' + l[i]);
		v.beginGroup(l[i]);
		addLauncher(l[i]);
		v.endGroup();
	      }
	    v.endGroup();
	    console.log('=================>FIN creation launchers');
	}
	function addLauncher(name) {
	    //console.log('adding Launcher: ' + name);
	    var objEmbeed = compt.createObject(launchers);
	    objEmbeed.appName=name;
	    objEmbeed.init(v);
	    objEmbeed.onHome.connect(onHomeAsked);
	}
	function onHomeAsked(){
	    console.log('showHome');
	    appControl.show();
	    console.log('FinShow');
	}
        function getLauncher(name) {
	    for(var i=0;i<children.length;i++){
	      var it = children[i];
	      if(name == children[i].appName)
		return children[i];
	    }
	}
    }
     
    
      
 }
 